package com.serviceImpl;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.xml.bind.JAXBElement;

import org.apache.struts2.ServletActionContext;

import com.bean.UserInfo;
import com.bean.Usin;
import com.dao.UsinDao;
import com.opensymphony.xwork2.ActionContext;
import com.util.MD5Util;

@Path("/usinService/")
public class UsinServiceImpl {
	
	private UsinDao usinDao;
	private String usid;
	private String pass;
	private Usin usins;
	private boolean flag;
	
	public String getUsid() {
		return usid;
	}

	public void setUsid(String usid) {
		this.usid = usid;
	}

	public String getPass() {
		return pass;
	}

	public void setPass(String pass) {
		this.pass = pass;
	}

	public Usin getUsins() {
		return usins;
	}

	public void setUsins(Usin usins) {
		this.usins = usins;
	}

	public UsinDao getUsinDao() {
		return usinDao;
	}

	public void setUsinDao(UsinDao usinDao) {
		this.usinDao = usinDao;
	}
	/**
	 * 判断用户登录信息是否正确
	 * @return
	 * http://localhost:8080/ScmSvc/service/usin/usinService/judgeUsinId
	 */
	@POST
	@Path("judgeUsinId")
	@Produces("application/json")
	public boolean findUserByUsid(@FormParam("usid") String usid,@FormParam("pass") String pass)
	{
		boolean flag = false;
		UserInfo u = new UserInfo();
		u.setUsid(usid);
		u.setPass(pass);
		List<Usin> list = logJudgeUsid(usid);
		if(list.size()>0){
			usins = list.get(0);
			String Pwd=usins.getPass();
			pass=MD5Util.string2MD5(usins.getUsid().toLowerCase() + "@#@" +  pass);
			if(Pwd.equalsIgnoreCase(pass.trim())){
				flag =true;
			}
		}
		return flag;
	}
	/**
	 * 查询用户ID 是否已经存在
	 * @return
	 * http://localhost:8080/ScmSvc/service/usin/usinService/existed/{usid}
	 */
	@GET
	@Path("existed/{usid}/{pass}")
	@Produces("application/json")
	@Consumes("application/json")
	public JAXBElement<UserInfo> IsUserExisted(@PathParam("usid") String usid, @PathParam("pass") String pass)
	{
		final String ROOT_NODE = "root";
		
		UserInfo u = new UserInfo();
		u.setName(usid);
		u.setPass(pass);
		
		javax.xml.namespace.QName qn = new javax.xml.namespace.QName("");
		JAXBElement<UserInfo> xu = new JAXBElement<UserInfo>(qn, UserInfo.class, u);
		
		return xu;
	}
	
	/**
	 * 判断是否是面料管理员
	 * @return
	 */
	public boolean isBmmgn(String usid){
		
		return usinDao.isBmmgn(usid);
	}
	
	public List<Usin> logJudgeUsid(String usid){
		return usinDao.findByProperty("usid", usid);
	}
	
	/**
	 * 验证码
	 * @return
	 */
 
	@Context 
	private HttpServletRequest request;  
	@POST
	@Path("logcheckCode")
	@Produces("application/json")
	public boolean checkCode(@FormParam("code") String code){
		String codeStr = (String)request.getSession().getAttribute("str");
		
		if(code.equalsIgnoreCase(codeStr)){
			flag=true;
		}else{
			flag=false;
		}
		return flag;
	}

}
